package com.henz.web.dao.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.henz.web.dao.HenzDao;
import com.henz.web.domain.HenzUser;

public class HenzDaoImpl implements HenzDao {
	
	private Log logger = LogFactory.getLog(this.getClass());

    private PersistenceManagerFactory persistenceManagerFactory;

	@Override
	public List<Map<String, Object>> saveUser(List<Map<String, Object>> userList) {
		logger.info("HenzDaoImpl saveUser[" + userList + "]");
		/*
		
		PersistenceManager persistenceManager = persistenceManagerFactory
				.getPersistenceManager();
		persistenceManager.currentTransaction().begin();
		Long id = null;
		
		logger.info("HenzDaoImpl saveUser[" + userList + "]");
		
		for( Map<String, Object> userMap: userList){
			
			HenzUser userObj = null;
			
			userObj = new HenzUser( userMap );
			
			id = 1l;
			
			logger.info("HenzDaoImpl id[" + id + "]");
			logger.info("HenzDaoImpl userObj[" + userObj + "]");
			
			
			userObj = persistenceManager.makePersistent(userObj);
			
			if( id == null ){
				userObj = persistenceManager.makePersistent(userObj);
			}else{
				HenzUser updatedUser = persistenceManager.getObjectById(
						HenzUser.class, id);
				updatedUser.setData(userMap);
				userObj = persistenceManager.makePersistent(userObj);
			}
			userMap = userObj.getUserMap();
		}
		persistenceManager.currentTransaction().commit();
		persistenceManager.close();
		*/
		return userList;
	}

	@Override
	public void deleteUser(HenzUser user) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteUserById(Long id) {
		// TODO Auto-generated method stub

	}

	@Override
	public HenzUser findUserById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<HenzUser> findAllUsers( Map<String, Object> paramMap ) {
        PersistenceManager persistenceManager = persistenceManagerFactory.getPersistenceManager();
        
        Query query = persistenceManager.newQuery(HenzUser.class);
        
        query.setOrdering("id descending");
        
        Collection<HenzUser> userList = (Collection<HenzUser>) query.execute();
        
        return userList;
	}

}
